package com.qiantu.phone.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLFloorDBManager;
import com.qiantu.api.entity.ElecPeriodBean;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.dialog.BottomAreaSelectDialog_Energy;
import com.qiantu.phone.ui.dialog.BottomCalendarSelectDialog;
import com.lxj.xpopup.XPopup;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EnergyStatisticsActivity extends AppActivity {
    private boolean mIsQueryHouse = true;
    private ArrayList<String> mRoomSerialNos = new ArrayList<>();
    EnergyItem energyItem1;
    EnergyItem energyItem2;
    EnergyItem energyItem3;
    private TextView tvAreaText;
    private View vDateSelect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_energy_statistics;
    }

    @Override
    protected void initView() {
        tvAreaText = findViewById(R.id.select_areas);
        vDateSelect = findViewById(R.id.select_date);
        energyItem1 = new EnergyItem(findViewById(R.id.energy_item_1), getString(R.string.today_energy));
        energyItem2 = new EnergyItem(findViewById(R.id.energy_item_2), getString(R.string.week_energy));
        energyItem3 = new EnergyItem(findViewById(R.id.energy_item_3), getString(R.string.month_energy));
        setOnClickListener(tvAreaText, vDateSelect);
    }

    @Override
    protected void initData() {
        tvAreaText.setText(R.string.whole_house);
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateData();
    }

    private void updateData(){
        LLHttpManager.getElecStatistics(this, mIsQueryHouse, AppApplication.getInstance().getCurrentHouseSerialNo(), (mIsQueryHouse ? null : mRoomSerialNos), new HttpCallback<HttpData<ElecPeriodBean>>(this){
            @Override
            public void onSucceed(HttpData<ElecPeriodBean> result) {
                super.onSucceed(result);
                ElecPeriodBean data = result.getData();
                energyItem1.setData(data.getToday());
                energyItem2.setData(data.getLastSevenDays());
                energyItem3.setData(data.getLastThirtyDays());
            }
        });
    }

    static class EnergyItem{
        TextView title;
        TextView electricity;
        TextView bell;
        EnergyItem(View parent, String name){
            title = parent.findViewById(R.id.title);
            title.setText(name);
            electricity = parent.findViewById(R.id.electricity);
            bell = parent.findViewById(R.id.e_bill);
        }

        public void setData(ElecPeriodBean.ElecBean data){
            electricity.setText(String.format("%.2f", data.elec));
            bell.setText(String.format("%.2f", data.bill));
        }
    }

    @Override
    public void onRightClick(View view) {
        super.onRightClick(view);
        startActivity(EnergyDillActivity.class);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == tvAreaText){
            showSelectAreaDialog();
        }else if (view == vDateSelect){
            showCalendarDialog();
        }
    }

    BottomAreaSelectDialog_Energy selectDialog;

    private void showSelectAreaDialog() {
        if (selectDialog == null) {
            selectDialog = new BottomAreaSelectDialog_Energy(getContext()) {
                @Override
                public void onSave(boolean isQueryHouse, Map<String, RoomBean> result) {
                    mIsQueryHouse = isQueryHouse;
                    if (isQueryHouse){
                        tvAreaText.setText(R.string.whole_house);
                    }else {
                        ArrayList<String> rooms = new ArrayList<>();
                        for (RoomBean bean : result.values()){
                            rooms.add(bean.getRoomSerialNo());
                        }
                        mRoomSerialNos = rooms;
                        StringBuilder buffer = new StringBuilder();
                        for (String str : selectDialog.getSelectAreaTitle().values()){
                            if (!TextUtils.isEmpty(str)){
                                if (buffer.length() == 0){
                                    buffer.append(str);
                                }else {
                                    buffer.append("、").append(str);
                                }
                            }
                        }
                        tvAreaText.setText(buffer.toString());
                    }
                    updateData();
                }
            };
            new XPopup.Builder(getContext())
                    .enableDrag(false)
                    .dismissOnBackPressed(true)
                    .asCustom(selectDialog);
        }
        //selectDialog.setInitCount(sceneAdapter.getItemCount());
        List<FloorBean> floors = LLFloorDBManager.getInstance(getContext()).getNotEmptyRoomFloors();
        selectDialog.setFloorBeans(floors);
        selectDialog.show();
    }

    BottomCalendarSelectDialog calendarSelectDialog;
    private void showCalendarDialog(){
        if (calendarSelectDialog == null){
            calendarSelectDialog = new BottomCalendarSelectDialog(getContext()){
                @Override
                public void onSave(String start, String end) {
                    super.onSave(start, end);
                    EnergyStatisticsDetailActivity.start(getAppActivity(), mIsQueryHouse, mRoomSerialNos, tvAreaText.getText().toString(),start, end);
                }
            };
            new XPopup.Builder(getContext())
                    .enableDrag(false)
                    .dismissOnBackPressed(true)
                    .asCustom(calendarSelectDialog);
        }
        calendarSelectDialog.show();
    }
}